import { Component, OnInit, EventEmitter, Output } from "@angular/core";
import { ClueDataService } from "../../../../../@core/service/clue-service/clue-data.service";
import { ClueService } from "../../../../../@core/service/typescript-angular-client/api/clue.service";
import {BizfileService} from '../../../../../@core/service/case-review/doc-service/api/bizfile.service';
@Component({
  selector: 'app-register-no-approval',
  templateUrl: './register-no-approval.component.html',
  styleUrls: ['./register-no-approval.component.scss']
})
export class RegisterNoApprovalComponent implements OnInit {
  @Output('loadSpin') loadSpin = new EventEmitter();
 
  public isReadonly:boolean = false;
  public nodes = [
    { title: "食品", value: "食品", key: "食品", isLeaf: true },
    { title: "餐饮", value: "餐饮", key: "餐饮", isLeaf: true },
    { title: "药品", value: "药品", key: "药品", isLeaf: true },
    { title: "保健食品", value: "保健食品", key: "保健食品", isLeaf: true },
    { title: "化妆品", value: "化妆品", key: "化妆品", isLeaf: true },
    { title: "医疗器械", value: "医疗器械", key: "医疗器械", isLeaf: true },
    { title: "广告", value: "广告", key: "广告", isLeaf: true },
    { title: "假冒伪劣", value: "假冒伪劣", key: "假冒伪劣", isLeaf: true },
    { title: "反不正当竞争",  value: "反不正当竞争", key: "反不正当竞争",isLeaf: true},
    { title: "商标", value: "商标", key: "商标", isLeaf: true },
    { title: "消费者投诉",value: "消费者投诉",key: "消费者投诉",isLeaf: true},
    { title: "无照经营", value: "无照经营", key: "无照经营", isLeaf: true }
  ];
  //
  private _registerDate:any=null;
  private _agentDate:any = new Date();
  BizFile: any; 
  // 加载动画
  loading: boolean;

  public get registerDate(){
    return this._registerDate
  }

  public set registerDate(val){
      if(val){
        let date:Date = new Date(val)
        this.formJson.registerDate = this.dateFtt("yyyy-MM-dd hh:mm:ss",date);
        this._registerDate = val;
      }
  }

  public get agentDate(){
    return this._agentDate;
  }

  public set agentDate(val){
      if(val){
        this._agentDate = val;
        let date:Date = new Date(val)
        this.formJson.yearAgent = date.getFullYear();
        this.formJson.monthAgent = date.getMonth()+1;
        this.formJson.dayAgent = date.getDate();
      }
  }

  public treeValue: any;
 
  public areaData: any;
  //省级联动
  public provinces: any[];
  //市级联动
  public cities: any[];
  //县级联动
  public counties: any[];
  //选择内容
  public addressDetails: any[] = [];
  public addressName: any[] = [];
  public sortValue: any = "0";
  public getSelectValue:string;

   //docid 如果是修改会传入docid
   public docid:string;
  //form内容
  public formJson: any = {
    registerContent: null,
    yearAgent:this._agentDate.getFullYear(),
    monthAgent:this._agentDate.getMonth()+1,
    dayAgent:this._agentDate.getDate(),
  };

  constructor(
    public clueData: ClueDataService,
    public clueService: ClueService,
    public bizService:BizfileService
  ) {
    this.areaData = clueData.area;
  }
ngOnInit() {
    if(this.docid){
   
      this.bizService.queryBizFileByDocid(this.docid).subscribe(({data}:any)=>{
         this.loadSpin.emit(false);
        if(data){

          if(data.stage) this.getSelectValue = data.stage;
          this.BizFile = data;
          this.formJson = JSON.parse(data.formJson);
          if(this.formJson.caseType){
            this.treeValue = JSON.parse(this.formJson.caseType);
          }
          this.registerDate = this.formJson.registerDate;
          this.agentDate = `${this.formJson.yearAgent}-${this.formJson.monthAgent}-${this.formJson.dayAgent}`
          let city,county; 
          //查找返回市级列表
          if(this.formJson.addressDetails.city){
            this.cities = this.areaData[0].children;
            city = this.formJson.addressDetails.city;
            this.addressDetails[1] = this.cities.filter(cityF=>cityF.key == city)[0].label
          }else{
            this.cities = [];
            city =null;
          }
          //查找返回县级列表
          if(this.formJson.addressDetails.county){
            this.counties = this.cities.filter(cityItem=>city ==cityItem.key)[0].children;
            county = this.formJson.addressDetails.county;
            this.addressDetails[2] = this.counties.filter(countyF=>county == countyF.key)[0].label
          }else{
            this.counties = [];
            county =null;
          }
          this.addressDetails[0] = city;
          this.addressDetails[1] = county;
          if(this.formJson.fontName||this.formJson.name||this.formJson.idNum){
            this.sortValue = '1';
          }
        }
      })
  }else{
    this.cities = this.areaData[0].children;
  }
  }
  //tree选择
  treeOnChange(event) {
    
  }

  //市级选择
  onCityChange(event) {
    if (event !== null) {
      let currCity = this.cities.filter(city => city.key == event);
      this.counties = currCity[0].children;
      this.addressDetails[0] = currCity[0].label;
      this.addressDetails[1] = null;
      this.addressDetails[2] = null;
      this.addressName[0] = currCity[0].label;
      this.addressName[1] =  this.addressName[2] = null;
    } else {
      this.addressDetails[0] = this.addressDetails[1] = "";
      this.counties = [];
    }
  }
  //县级选择
  onCountyChange(event) {
    if (event !== null) {
      let currCounty = this.counties.filter(county => county.key == event);
      this.addressDetails[1] = currCounty[0].label;
      this.addressName[1] = currCounty[0].label;
      this.addressDetails[2] = null;
    } else {
      this.addressDetails[1] = null;
    }
  }
  selectChange(event){
    this.getSelectValue = event;
  }
  //日期格式化
  dateFtt(fmt,date) 
      { //author: meizz 
      var o = { 
      "M+" : date.getMonth()+1,     //月份 
      "d+" : date.getDate(),     //日 
      "h+" : date.getHours(),     //小时 
      "m+" : date.getMinutes(),     //分 
      "s+" : date.getSeconds(),     //秒 
      "q+" : Math.floor((date.getMonth()+3)/3), //季度 
      "S" : date.getMilliseconds()    //毫秒 
      }; 
      if(/(y+)/.test(fmt)) 
      fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 
      for(var k in o) 
      if(new RegExp("("+ k +")").test(fmt)) 
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 
      return fmt; 
      } 
}
